//
//  HEFriendsViewController.m
//  huien
//
//  Created by yiban on 2017/7/6.
//  Copyright © 2017年 lcb. All rights reserved.
//

#import "HEFriendsViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
@interface HEFriendsViewController ()<UIActionSheetDelegate>
{
    
}

@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel* ycodeVLabel;
@property(nonatomic,strong)LcbBaseModel *baseModel;
@end

@implementation HEFriendsViewController
@synthesize imageView=_imageView;
@synthesize ycodeVLabel=_ycodeVLabel;
@synthesize baseModel=_baseModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle = @"邀请好友";
    [self initTableView];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)initTableView
{
    //
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 60, 44);
    backButton.tag =100;
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [backButton addTarget:self action:@selector(clickNavEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:backButton];
    [self initUI];
}
-(void)clickNavEvent:(id)sender
{
    if (self.navigationController) {
        if (self.navigationController.viewControllers.count == 1) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        } else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
}
-(void)initUI
{
    UIView *backView =[[UIView alloc] initWithFrame:CGRectMake(0, self.headerView.frame.size.height, SCREEN_WIDTH, self.view.frame.size.height-self.headerView.frame.size.height)];
    backView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:backView];
    //
    UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, backView.frame.size.width-20, 40)];
    titleLabel.text = @"尊敬的惠恩资本理财网会员，您可以通过邀请好友注册惠 恩资本理财网获得惠恩资本理财网奖金。";
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.textColor = h1color;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.numberOfLines=2;
    [backView addSubview:titleLabel];
    //
    UIView *line1 =[[UIView alloc] initWithFrame:CGRectMake(0, titleLabel.frame.origin.y + titleLabel.frame.size.height+20, backView.frame.size.width, 1)];
    line1.backgroundColor=linecolor;
    [backView addSubview:line1];
    
    UILabel* ycodeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,line1.frame.origin.y+line1.frame.size.height+10, 80, 20)];
    ycodeLabel.text = @"您的邀请码";
    ycodeLabel.font = [UIFont systemFontOfSize:15];
    ycodeLabel.textColor = h1color;
    ycodeLabel.backgroundColor = [UIColor clearColor];
    [backView addSubview:ycodeLabel];
    
    UIView *line2 =[[UIView alloc] initWithFrame:CGRectMake(ycodeLabel.frame.origin.x+ycodeLabel.frame.size.width+10, line1.frame.origin.y, 1, 40)];
    line2.backgroundColor=linecolor;
    [backView addSubview:line2];
    
    _ycodeVLabel = [[UILabel alloc] initWithFrame:CGRectMake(line2.frame.origin.x+line2.frame.size.width+10, ycodeLabel.frame.origin.y, backView.frame.size.width-20, 20)];
    _ycodeVLabel.text = @"";
    _ycodeVLabel.font = [UIFont systemFontOfSize:15];
    _ycodeVLabel.textColor = h1color;
    _ycodeVLabel.backgroundColor = [UIColor clearColor];
    [backView addSubview:_ycodeVLabel];
    UIView *line3 =[[UIView alloc] initWithFrame:CGRectMake(0, ycodeLabel.frame.origin.y+ycodeLabel.frame.size.height+10, backView.frame.size.width, 1)];
    line3.backgroundColor=linecolor;
    [backView addSubview:line3];
    //
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTapAction:)];
    longPress.minimumPressDuration = 0.8; //定义按的时间
    longPress.numberOfTouchesRequired = 1;
    _imageView = [[UIImageView alloc] initWithFrame: CGRectMake((backView.frame.size.width-180)/2, line3.frame.origin.y+line3.frame.size.height+40, 180, 180)];
    _imageView.userInteractionEnabled=YES;
    [backView addSubview:_imageView];
    [_imageView addGestureRecognizer:longPress];
    longPress=nil;
    //
    UILabel* tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(_imageView.frame.origin.x, _imageView.frame.origin.y+_imageView.frame.size.height+10, 180, 40)];
    tagLabel.text = @"扫一扫快速推荐或长按保存到相册分享";
    tagLabel.font = [UIFont systemFontOfSize:14];
    tagLabel.textColor = UIColorFromRGB(0x9d817d);
    tagLabel.backgroundColor = [UIColor clearColor];
    tagLabel.numberOfLines=2;
    [backView addSubview:tagLabel];
    
    //
    UIButton *shareBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn.frame=CGRectMake(20, backView.frame.size.height -45*2,  backView.frame.size.width-40, 45);
    shareBtn.backgroundColor =btncolor;
    shareBtn.layer.cornerRadius = 45/2;
    shareBtn.tag= 1;
    [shareBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [shareBtn setTitle:@"邀请好友" forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(loginbtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:shareBtn];
}
- (void) longTapAction:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan && self.imageView.image!=nil) {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"保存图片" otherButtonTitles:@"查看图片", nil];
        [sheet showInView:self.view];
       
    }
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
       
        case 0:
        {
            NSString *tipTextWhenNoPhotosAuthorization; // 提示语
            // 获取当前应用对照片的访问授权状态
            ALAuthorizationStatus authorizationStatus = [ALAssetsLibrary authorizationStatus];
            // 如果没有获取访问授权，或者访问授权状态已经被明确禁止，则显示提示语，引导用户开启授权
            if (authorizationStatus == ALAuthorizationStatusRestricted || authorizationStatus == ALAuthorizationStatusDenied) {
                NSDictionary *mainInfoDictionary = [[NSBundle mainBundle] infoDictionary];
                NSString *appName = [mainInfoDictionary objectForKey:@"CFBundleDisplayName"];
                tipTextWhenNoPhotosAuthorization = [NSString stringWithFormat:@"请在设备的\"设置-隐私-照片\"选项中，允许%@访问你的手机相册", appName];
                // 展示提示语
                return;
            }
            
            __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
            [lib writeImageToSavedPhotosAlbum:self.imageView.image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
                
                NSLog(@"assetURL = %@, error = %@", assetURL, error);
                lib = nil;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [YBToast showWithText:@"成功保存到相册!"];
                });
              
            }];
            
            
        }
            break;
        case 1:
        {
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:imagePickerController animated:YES completion:nil];
         
        }break;
      
        default:
            break;
    }
}
-(void)loginbtnClick:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    switch (btn.tag) {
        case 0:
        {
            
        }
            break;
        case 1:
        {
            if([self.baseModel.data isKindOfClass:[NSDictionary class]])
            {
                [[JGShare sharedJGShare] shareWhitParams:self.baseModel.data];
            }
            
        }
            break;
            
        default:
            break;
    }
}
-(void)loadData
{
    SessionModel *sessionModel =[HEShare sessionModel];
    __weak typeof (self) wSelf = self;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:sessionModel.sessionId forKey:@"sessionId"];
    [dict setValue:[NSString stringWithFormat:@"%ld",(long)sessionModel.session_user.userId] forKey:@"userId"];
    
    [HEApi promoteLinksWithParam:dict serviceblock:^(id response, NSString *errorMsg) {
        if (response) {
            if ([response isKindOfClass:[LcbBaseModel class]]) {
                LcbBaseModel *baseModel = (LcbBaseModel*)response;
                if (baseModel.code>0) {
                    [[NativeRouter sharedNativeRouter] codeHander:wSelf params:baseModel completion:nil];
                }else{
                    wSelf.baseModel = baseModel;
                    if([baseModel.data isKindOfClass:[NSDictionary class]])
                    {
                        _ycodeVLabel.text =baseModel.data[@"ycode"];
                        [_imageView lcb_imageWithUrlStr:baseModel.data[@"qrCode"] placeholder:[UIImage imageNamed:@"user"]];
                    }
                    
                }
                
            }
        }
        
    }];
}
@end
